//处理模型
const {
    successModel,
    failModel
} = require('../model/responseModel')

const {
    getCart,
    updateCart,
    syncCart,
    getCollection,
    updateCollection,
    updateViewed,
    getViewed,
    updateUserAddress,
    getUserAddress,
    getUserOneAddress,
    addUserAddress,
    isValidPassword,
    updatePassword,
    hasPayPassword,
    isValidPayPassword,
    setPayPassword,
    setNickname,
deleteUserAddress

} = require('../controller/user')


//处理用户相关的路由
const handleUserRoute = (req, res) => {
    //定义处理路由的逻辑
    const method = req.method;
    console.log(req.query,req.body, req.path,"path");
    switch (method) {

        case 'POST': {

            //同步未登录前购物车数据
            if (req.path === '/user/update_cart') {
                console.log('同步未登录前购物车数据',req.body,"同步未登录前购物车数据");
                return updateCart(req.body).then((data) => {
                    if (data)
                        return new successModel('更新未登录前购物车成功')
                    return new failModel('更新未登录购物车失败')
                })

            //登录后同步购物车数据
            }else  if (req.path === '/user/sync_cart') {

                return syncCart(req.body).then((data) => {
                    if (data)
                        return new successModel('更新购物车成功')
                    return new failModel('更新购物车失败')
                })

            //更新收藏信息
            }else if (req.path === '/user/update_collection') {

                return updateCollection(req.body).then((data) => {
                    if (data)
                        return new successModel('更新收藏信息成功')
                    return new failModel('更新收藏信息失败')
                })
                
            //更新浏览记录
            }else if (req.path === '/user/update_viewed') {

                return updateViewed(req.body).then((data) => {
                    if (data)
                        return new successModel('更新浏览记录成功')
                    return new failModel('更新浏览记录失败')
                })

            //添加收货地址
            }else if(req.path === '/user/add_address') {

                return addUserAddress(req.body).then((data) => {
                    if (data)
                        return new successModel('添加收货地址成功')
                    return new failModel('添加收货地址失败')
                })

            //更新收货地址
            }else if(req.path === '/user/update_address') {

                return updateUserAddress(req.body).then((data) => {
                    if (data)
                        return new successModel('更改收货地址成功')
                    return new failModel('更改收货地址失败')
                })

            }
            
            //密码是否正确
            else if(req.path === '/user/valid_pwd') {

                return isValidPassword(req.body).then((data) => {
                    if (data)
                        return new successModel('密码正确')
                    return new failModel('密码错误')
                })

            }
            
            //更新登录密码
            else if(req.path === '/user/update_pwd') {

                return updatePassword(req.body).then((data) => {
                    if (data)
                        return new successModel('更新登录密码成功')
                    return new failModel('更新登录密码失败')
                })

            }
            //是否已有支付密码
            else if(req.path === '/user/has_pay_pwd') {

                return hasPayPassword(req.body).then((data) => {
                    if (data)
                        return new successModel('已有支付密码')
                    return new failModel('未有支付密码')
                })

            }
            //支付密码是否正确
            else if(req.path === '/user/valid_pay_pwd') {

                return isValidPayPassword(req.body).then((data) => {
                    if (data)
                        return new successModel('支付密码正确')
                    return new failModel('支付密码错误')
                })

            }
            //设置支付密码
            else if(req.path === '/user/set_pay_pwd') {

                return setPayPassword(req.body).then((data) => {
                    if (data)
                        return new successModel('设置支付密码成功')
                    return new failModel('设置支付密码失败')
                })

            }
            
            //设置昵称
            else if(req.path === '/user/set_nickname') {

                return setNickname(req.body).then((data) => {
                    if (data)
                        return new successModel('设置昵称成功')
                    return new failModel('设置昵称失败')
                })

            }else if (req.path === '/user/delete_address'){
                return deleteUserAddress(req.body).then((data) => {
                    if (data)
                        return new successModel('删除地址成功')
                    return new failModel('删除地址失败')
                })
            }
            
            
            
            
            
            
            
            else{
                return false;
            }
                
            
            
            
        };
        case 'GET': {
    
            //获取购物车数据
            if (req.path === '/user/cart') {
                return getCart(req.query).then(cartData => {
                    if (cartData) {
                        return new successModel(cartData)
                    } else {
                        return new failModel('获取购物车数据失败')
                    }
                })
            //获取收藏数据
            }else if (req.path === '/user/collection') {
                return getCollection(req.query).then(collectionData => {
                    if (collectionData) {
                        return new successModel(collectionData)
                    } else {
                        return new failModel('获取收藏信息失败')
                    }
                })
            //获取浏览数据
            }else if (req.path === '/user/viewed') {
                return getViewed(req.query).then(viewedData => {
                    if (viewedData) {
                        return new successModel(viewedData)
                    } else {
                        return new failModel('获取浏览记录失败')
                    }
                })
            //获取收货地址
            }else if (req.path === '/user/address') {
                return getUserAddress(req.query).then(addressdData => {
                    if (addressdData) {
                        return new successModel(addressdData)
                    } else {
                        return new failModel('获取收货地址失败')
                    }
                })
                
            //获取单条收货地址
            }else if (req.path === '/user/one_address') {
                return getUserOneAddress(req.query).then(addressdData => {
                    if (addressdData) {
                        return new successModel(addressdData)
                    } else {
                        return new failModel('获取收货地址失败')
                    }
                })
            }
            
            else{
                return false;
            }
    
        };
    default: {
        return false;
    }
    }

}

module.exports = handleUserRoute